package modelLayer;

import java.io.IOException;
import java.util.ArrayList;

import uiLayer.SaveToFile;

public class SLIRecords {
	private static SLIRecords instance;
	private ArrayList<SaleLinesItem> slis;
	private ArrayList<SaleLinesItem> collections;
	private int collectionId;
	
	public SLIRecords(){
		slis = new ArrayList<SaleLinesItem>();
		collectionId = 1;
		collections = new ArrayList<SaleLinesItem>();
	}
	
public void createSLI(Product product){
	SaleLinesItem sli = new SaleLinesItem(product);
	slis.add(sli);
}

/**
 * @return the collections
 */
public ArrayList<SaleLinesItem> getCollections() {
	return collections;
}

public String createCollection(String name){
	 
	String ID = "c"+collectionId;
	SaleLinesItem sl = new SaleLinesItem(ID,name);
	slis.add(sl);
	collections.add(sl);
	collectionId++;
	return ID;
	
}
public void saveSLI() throws IOException{
	for(SaleLinesItem s1: slis){
		if(s1.save()!="")SaveToFile.getInstance().save(s1.save());
	}
	}


/**
 * @param collectionId the collectionId to set
 */
public void setCollectionId(int collectionId) {
	this.collectionId = collectionId;
}

public ArrayList<SaleLinesItem> getSlis() {
	return slis;
}

public void setSlis(ArrayList<SaleLinesItem> slis) {
	this.slis = slis;
}

public void addToSLI(int id, String collectionID){
	Product p1 = null;
	for(Product product: ProductRecords.getInstance().getProducts()){
		
		if (product.getId()==id)p1=product;
	
	for(SaleLinesItem sli:slis){
		if(sli.getID().equals(collectionID)){
			
			sli.getItemCollection().add(p1);
		}
	}
	
	}
}

public static SLIRecords getInstance() {
	if(instance == null){
		instance = new SLIRecords();
	}
	return instance;
}
public SaleLinesItem getSaleLinesItem(String ID){
	for(SaleLinesItem sli: slis){
		if(sli.getID().equals(ID))return sli;
	}
	return null;
}
}


